import 'package:day_demo/src/fake_wx_drag_dismiss/fake_wx_drag_dismiss.dart';
import 'package:day_demo/src/logger_helper.dart';
import 'package:dismissible_page/dismissible_page.dart';
import 'package:flutter/material.dart';

class HeroStateWidget extends StatefulWidget {
  const HeroStateWidget({
    super.key,
    required this.tag,
  });
  final String tag;
  @override
  State<HeroStateWidget> createState() => _HeroStateWidgetState();
}

class _HeroStateWidgetState extends State<HeroStateWidget> {
  void addStatusListener(AnimationStatus status) {
    LogHelper.d(status);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Hero(
        tag: widget.tag,
        flightShuttleBuilder: (flightContext, animation, flightDirection,
            fromHeroContext, toHeroContext) {
          if (flightDirection == HeroFlightDirection.push) {
            /// only add listener once
            animation.addStatusListener((status) {
              addStatusListener(status);
            });
          }

          return toHeroContext.widget;
        },
        child: Material(
          type: MaterialType.transparency,
          child: PageView(
            physics: const BouncingScrollPhysics(),
            scrollDirection: Axis.vertical,
            children: List.generate(
              10,
              (index) => Stack(
                children: [
                  Image.network(
                    'https://source.unsplash.com/1900x3600/?camera,paper',
                    fit: BoxFit.fill,
                    width: MediaQuery.of(context).size.width,
                    height: MediaQuery.of(context).size.height,
                  ),
                  Row(
                    children: [
                      Flexible(
                        child: Container(
                          color: Colors.transparent,
                          width: double.infinity,
                          height: 100,
                        ),
                      )
                    ],
                  ),
                  Positioned(
                      left: 10,
                      top: MediaQuery.of(context).padding.top,
                      child: ElevatedButton(
                        onPressed: () {
                          DragDismissController.instance.close();
                        },
                        child: const Icon(Icons.arrow_back_ios),
                      )),
                  Positioned(
                    left: 0,
                    right: 0,
                    bottom: 0,
                    child: SingleChildScrollView(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.end,
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          const SizedBox(
                            width: 100,
                            height: 100,
                            child: ColoredBox(color: Colors.deepOrangeAccent),
                          ),
                          Flexible(
                            child: ListView(
                              shrinkWrap: true,
                              children: [
                                configHeader(context),
                                _configFooterView(context),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget configHeader(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 4, left: 16),
      child: Row(
        children: [
          Flexible(
            child: SizedBox(
              width: 100,
              height: 100,
              child: Container(
                color: Colors.orange,
              ),
            ),
          ),
          Expanded(
            child: Row(
              children: [
                Expanded(
                  child: Container(
                    padding: const EdgeInsets.only(left: 16, right: 8),
                    constraints:
                        const BoxConstraints(minWidth: 50, maxWidth: 150),
                    child: const Material(
                      color: Colors.transparent,
                      child: Text(
                        'dsssssssssbbbbbs',
                        maxLines: 1,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

Widget _configFooterView(BuildContext context) {
  return Container(
    color: Colors.red,
    padding: const EdgeInsets.fromLTRB(16, 8, 0, 30),
    child: Material(
      type: MaterialType.transparency,
      child: Row(
        children: [
          Expanded(
            flex: 1,
            child: Container(
              padding: const EdgeInsets.symmetric(vertical: 5, horizontal: 0),
              decoration: BoxDecoration(
                color: Colors.blue,
                borderRadius: BorderRadius.circular(8),
              ),
              child: const Material(
                type: MaterialType.transparency,
                child: Text(
                  "gggggggg...",
                  maxLines: 1,
                ),
              ),
            ),
          ),
          const Flexible(child: Card(child: ButtonContinaer())),
        ],
      ),
    ),
  );
}

class ButtonContinaer extends StatelessWidget {
  const ButtonContinaer({super.key});

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Flexible(
            flex: 2,
            child: Padding(
                padding: EdgeInsets.only(left: 5, right: 5),
                child: Text('收藏'))),
        Flexible(flex: 2, child: Text("data")),
        Flexible(flex: 2, child: Text('收藏')),
      ],
    );
  }
}

class HeroWxPage extends StatelessWidget {
  const HeroWxPage({
    super.key,
    required this.tag,
  });
  final String tag;
  @override
  Widget build(BuildContext context) {
    LogHelper.d('HeroWxPage');
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: PageView(
        physics: const BouncingScrollPhysics(),
        scrollDirection: Axis.vertical,
        children: List.generate(
          10,
          (index) => Stack(
            children: [
              Image.network(
                'https://source.unsplash.com/1900x3600/?camera,paper',
                fit: BoxFit.fill,
                width: MediaQuery.of(context).size.width,
                height: MediaQuery.of(context).size.height,
              ),
              Positioned(
                  left: 10,
                  top: MediaQuery.of(context).padding.top,
                  child: ElevatedButton(
                    onPressed: () {
                      DragDismissController.instance.close();
                    },
                    child: const Icon(Icons.arrow_back_ios),
                  ))
            ],
          ),
        ),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  const SecondPage({super.key});

  @override
  Widget build(BuildContext context) {
    return DismissiblePage(
      onDismissed: () {
        Navigator.of(context).pop();
      },
      // Note that scrollable widget inside DismissiblePage might limit the functionality
      // If scroll direction matches DismissiblePage direction
      direction: DismissiblePageDismissDirection.multi,
      isFullScreen: false,
      child: Hero(
        tag: 'HeroWxPage',
        child: Image.network(
          'https://source.unsplash.com/1900x3600/?camera,paper',
          fit: BoxFit.cover,
        ),
      ),
    );
  }
}
